package com.guinea.account.web;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.navigator.menu.MenuRepository;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.guinea.account.entity.Menu;
import com.guinea.account.form.MenuForm;
import com.guinea.account.service.MenuManager;
import com.guinea.account.service.RoleManager;
import com.guinea.core.service.CRUDManager;
import com.guinea.core.web.StrutsEntityAction;

public class MenuAction extends StrutsEntityAction {

	private MenuManager menuManager;

	public CRUDManager getEntityManager() {
		return (CRUDManager) getMenuManager();
	}

	public MenuManager getMenuManager() {
		return menuManager;
	}

	public void setMenuManager(MenuManager menuManager) {
		this.menuManager = menuManager;
	}

	private RoleManager roleManager;
	
	public RoleManager getRoleManager() {
		return roleManager;
	}

	public void setRoleManager(RoleManager roleManager) {
		this.roleManager = roleManager;
	}

	
	/**
	 * 删除单个对象的Action函数.
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Object object = initEntity(request);
		if(object == null){
			saveDirectlyError(request, "操作失败：记录不存在");
			return list(mapping, form, request, response);
		}
		try {
			doDeleteEntity(request, object);
			saveMessage(request, "entity.deleted");
		} catch (Exception e) {
			saveDirectlyError(request, e.getMessage());
		}
		return mapping.findForward(SUCCESS);
	}
	
	/**
	 * 显示Form表单时的回调函数.为Form对象添加更多属性,在子类重载.
	 */
	protected void onInitForm(ActionForm form, HttpServletRequest request,
			Object object) {
		if (id != null) {
			MenuForm menuForm = (MenuForm) form;
			MenuManager menuManager = getMenuManager();
			menuForm.setChildrenIds(menuManager.getChildrenIds(id));
			menuForm.setRelativeRoleIds(menuManager.getRelativeRoleIds(id));
		}
	}

	/**
	 * form与list界面所需的参考对象注入.如categoryList,在子类重载.
	 */
	public void refrenceData(ActionForm form, HttpServletRequest request,
			Object object) {
		request.setAttribute("allMenus", getMenuManager().getValid());
	}

	/**
	 * 保存Form表单时的回调函数.为业务对象添加更多属性，在子类重载.
	 */
	public void onInitEntity(ActionForm form, HttpServletRequest request,
			Object object) {
		Menu menu = (Menu) object;
        String[] relativeRoleIds = request.getParameterValues("relativeRoleIds");
        menu.getRoles().clear();
        for (int i = 0; relativeRoleIds != null &&  i < relativeRoleIds.length; i++) {
            Long roleId = new Long(relativeRoleIds[i]);
            menu.getRoles().add(roleManager.get(roleId));
        }
		//设置parentId默认值
		String s = request.getParameter("parentId");
		if(s == null)
			menu.setParentId(new Long(0L));
	}
	/**
	 * 保存业务对象的函数.
	 */
	public void doSaveEntity(HttpServletRequest request,
			Object object) {
		getMenuManager().save(object);
		refreshRepository(request);
	}

	/**
	 * 标记删除业务对象的函数.
	 */
	public void doEraseEntity(HttpServletRequest request,
			Object object) {
		getMenuManager().save(object);
		refreshRepository(request);
	}

	/**
	 * 删除业务对象的函数.
	 */
	public void doDeleteEntity(HttpServletRequest request,
			Object object) {
		getMenuManager().delete(object);
		refreshRepository(request);
	}
	
	private void refreshRepository(HttpServletRequest request){
		ServletContext servletContext = request.getSession().getServletContext();
		servletContext.removeAttribute("repository");
		MenuRepository repository = ((MenuManager)getEntityManager()).getRepository();
		MenuRepository defaultRepository = (MenuRepository) servletContext
				.getAttribute(MenuRepository.MENU_REPOSITORY_KEY);
		repository.setDisplayers(defaultRepository.getDisplayers());
		servletContext.setAttribute("repository", repository);
		
		servletContext.removeAttribute("menus");
		List menus = getMenuManager().getValid();
		servletContext.setAttribute("menus",menus);
	}
}
